<script setup lang="ts">
import { ref } from 'vue'
import KnowledgeCard from './KnowledgeCard.vue'
import type { KnowledgeType, KnowledgeList } from '@/types/consult'
import { queryFoodAPI } from '@/api/consult'
// import { onMounted } from 'vue'
const props = defineProps<{
  type: KnowledgeType
}>()
const list = ref<KnowledgeList>([])
const loading = ref(false)
const finished = ref(false)
const params = {
  type: props.type,
  current: 1,
  pageSize: 10
}
const onLoad = async () => {
  // 异步更新数据
  const { data: res } = await queryFoodAPI(params)
  console.log(res)
  list.value.push(...res.rows)
  // 加载状态结束
  loading.value = false

  // 数据全部加载完成
  if (list.value.length === res.total) {
    finished.value = true
  } else {
    params.current++
  }
}
</script>

<template>
  <van-list
    v-model:loading="loading"
    :finished="finished"
    finished-text="没有更多了"
    @load="onLoad"
  >
    <div class="knowledge-list">
      <knowledge-card v-for="item in list" :key="item.id" :item="item"></knowledge-card>
    </div>
  </van-list>
</template>

<style lang="scss" scoped>
.knowledge-list {
  padding: 0 15px;
}
</style>
